package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.query.AnswerQueryObject;
import cn.wolfcode.trip.base.query.QuestionQueryObject;
import cn.wolfcode.trip.base.service.IAnswerService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("answer")
public class AnswerController {
    @Autowired
    private IAnswerService answerService;

    /**
     * 根据用户id查询问题数据
     * @param qo
     * @return
     */
    @GetMapping("{userId}")
    public PageInfo getAnswerById(AnswerQueryObject qo){
        //设置问题倒序查询
        qo.setOrderBy("a.createTime desc");
        qo.setPageSize(0);
        return answerService.queryByUserId(qo);
    }

    /**
     * 根据用户id查询问题数据
     * @param qo
     * @return
     */
    @GetMapping("/question/{questionId}")
    public PageInfo getAnswerByQuestionId(AnswerQueryObject qo){
        //设置问题倒序查询
        qo.setPageSize(7);
        qo.setOrderBy("a.createTime desc");
        return answerService.queryByQuestionId(qo);
    }
    /**
     * 根据问题id新增回答
     * @param
     * @return
     */
    @PostMapping("/save")
    public JSONResult saveAnswer(Answer answer){
        //新增
        answerService.save(answer);
        return new JSONResult();
    }

}
